package com.ys.code.generator.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;

public class FileUtil {
	public static void copyFile(String resFilePath, String distFolder) throws IOException {
		File resFile = new File(resFilePath);
		File distFile = new File(distFolder);
		if (resFile.isDirectory())
			FileUtils.copyDirectoryToDirectory(resFile, distFile);
		else if (resFile.isFile())
			FileUtils.copyFileToDirectory(resFile, distFile, true);
	}

	public static void deleteFile(String targetPath) throws IOException {
		File targetFile = new File(targetPath);
		if (targetFile.isDirectory())
			FileUtils.deleteDirectory(targetFile);
		else if (targetFile.isFile())
			targetFile.delete();
	}

	public static long genFileSize(String distFilePath) {
		File distFile = new File(distFilePath);
		if (distFile.isFile())
			return distFile.length();
		if (distFile.isDirectory())
			return FileUtils.sizeOfDirectory(distFile);

		return -1;
	}

	public static boolean isExist(String filePath) {
		return new File(filePath).exists();
	}

	public static String[] listFilebySuffix(String folder, String suffix) {
		IOFileFilter fileFilter1 = new SuffixFileFilter(suffix);
		IOFileFilter fileFilter2 = new NotFileFilter(DirectoryFileFilter.INSTANCE);
		FilenameFilter filenameFilter = new AndFileFilter(fileFilter1, fileFilter2);
		return new File(folder).list(filenameFilter);
	}

	public static boolean string2File(String res, String filePath) {
		boolean flag = true;
		try {
			res = new String(res.getBytes("UTF-8"));
			File file = new File(filePath);

			if (!(file.getParentFile().exists())) {
				file.getParentFile().mkdirs();
			}

			FileOutputStream fis = new FileOutputStream(file);
			OutputStreamWriter isr = new OutputStreamWriter(fis, "UTF-8");

			OutputStream out = new FileOutputStream(file);
			BufferedWriter rd = new BufferedWriter(isr);
			rd.write(res);
			rd.close();
			out.close();
			System.out.println(filePath);
		} catch (Exception e) {
			flag = false;
		}

		return flag;
	}

	public static String getClassPath() {
		System.out.println(FileUtil.class.getResource("/").getPath());
		return FileUtil.class.getResource("/").getPath();
	}
	
	
	public static boolean createCardImgZip(String sourcePath, String zipName) {
		// TODO Auto-generated method stub
		boolean result = false;
		String zipPath = sourcePath;
		File sourceFile = new File(sourcePath);
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		ZipOutputStream zos = null;

		if (sourceFile.exists() == false) {
			System.out.println("File catalog:" + sourcePath + "not exist!");
		} else {
			try {
				if(!new File(zipPath).exists()){
					new File(zipPath).mkdirs();
				}
				File zipFile = new File(zipPath + "/" + zipName + ".zip");
				if (zipFile.exists()) {
					System.out.println(zipPath + "Catalog File: " + zipName + ".zip" + "pack file.");
				} else {
					File[] sourceFiles = sourceFile.listFiles();
					if (null == sourceFiles || sourceFiles.length < 1) {
						System.out.println("File Catalog:" + sourcePath + "nothing in there,don't hava to compress!");
					} else {
						fos = new FileOutputStream(zipFile);
						zos = new ZipOutputStream(new BufferedOutputStream(fos));
						byte[] bufs = new byte[1024 * 10];
						for (int i = 0; i < sourceFiles.length; i++) {
							// create .zip and put pictures in
							ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
							zos.putNextEntry(zipEntry);
							// read documents and put them in the zip
							fis = new FileInputStream(sourceFiles[i]);
							bis = new BufferedInputStream(fis, 1024 * 10);
							int read = 0;
							while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
								zos.write(bufs, 0, read);
							}
						}
						result = true;
					}
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			} finally {
				try {
					if (null != bis)
						bis.close();
					if (null != zos)
						zos.close();
				} catch (IOException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}
		return result;
	}
}